package Fly.example.controller;


import Fly.example.common.R;
import Fly.example.pojo.Category;
import Fly.example.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author 陽Fly
 * @since 2025-06-13
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    ICategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize) {
        log.info("菜品及套餐分类查询开始…………");
        Page pageInfo = new Page(page, pageSize);//分页构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //根据排序排序,小到大
        wrapper.orderByAsc(Category::getSort);
        //调用service层的方法来进行分页
        categoryService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    //编辑页面反查详情接口
    @GetMapping("/{id}")
    public R<Category> seeByid(@PathVariable Integer id) {
        return R.success(categoryService.getById(id));
    }

    // 删除当前列的接口
    @DeleteMapping
    public R<String> deByid(@RequestParam("ids")String ids) {
        log.info("删除当前列的业务开始…………");
        categoryService.remove(ids);
        return R.success("删除成功");
    }

    // 修改接口
    @PutMapping
    public R<String> setByid(@RequestBody Category category) {
        log.info("修改业务开始…………");
        categoryService.updateById(category);
        return R.success("修改菜品数据成功");
    }

    // 新增接口
    @PostMapping
    public R<String> Insert(@RequestBody Category category) {
        log.info("新增菜品业务开始…………");
        categoryService.save(category);
        return R.success("新增菜品成功");
    }

    // 获取菜品分类列表
    //这个接口是用于添加菜品时用于页面的数据回显的
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        log.info("获取菜品分类列表业务开始…………");
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (category != null && category.getId() != null) {
            wrapper.eq(Category::getType, category.getType());
        }
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);

        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }

}
